import { ElMessage } from 'element-plus';

export function message(msg, type) {
    ElMessage({
        message: msg,
        showClose: true,
        type: type,
        center: true
    })
}

export const playMusicByChar = (keyCode) => {
    let fileName = "";
    switch (keyCode) {
        case 49:
            fileName = "/static/40-C-小字1组.mp3";
            break;
        case 50:
            fileName = "/static/42-D-小字1组.mp3";
            break;
        case 51:
            fileName = "/static/44-E-小字1组.mp3";
            break;
        case 52:
            fileName = "/static/45-F-小字1组.mp3";
            break;
        case 53:
            fileName = "/static/47-G-小字1组.mp3";
            break;
        case 54:
            fileName = "/static/49-A-小字1组.mp3";
            break;
        case 55:
            fileName = "/static/51-B-小字1组.mp3";
            break;

        case 81:
            fileName = "/static/28-C-小字组.mp3";
            break;
        case 87:
            fileName = "/static/30-D-小字组.mp3";
            break;
        case 69:
            fileName = "/static/32-E-小字组.mp3";
            break;
        case 82:
            fileName = "/static/33-F-小字组.mp3";
            break;
        case 84:
            fileName = "/static/35-G-小字组.mp3";
            break;
        case 89:
            fileName = "/static/37-A-小字组.mp3";
            break;
        case 85:
            fileName = "/static/39-B-小字组.mp3";
            break;
        default:
            return;
    }
    var audio = document.createElement("audio");
    audio.src = fileName;
    audio.play();
};

const intervalLong = 1000;
const intervalShort = 500;

export const playLittleStar = () => {
    const keyCodeList = [
        100, 49, 49, 53, 53, 54, 54, 53,
        100, 52, 52, 51, 51, 50, 50, 49,
        100, 53, 53, 52, 52, 51, 51, 50,
        100, 53, 53, 52, 52, 51, 51, 50,
        100, 49, 49, 53, 53, 54, 54, 53,
        100, 52, 52, 51, 51, 50, 50, 49
    ];

    delay(keyCodeList);
}

async function delay(keyCodeList) {
    for (var i = 0; i < keyCodeList.length; i++) {
        if (keyCodeList[i] == 100) {
            await sleep((intervalLong - intervalShort));
        } else {
            await sleep(intervalShort);
            playMusicByChar(keyCodeList[i]);
        }
    }
}

async function sleep(time) {
    await new Promise((resolve) => setTimeout(resolve, time));
}
